<?php

/**
 * Created by PhpStorm.
 * User: yellow
 * Date: 2017/6/28
 * Time: 10:46
 */

namespace Admin\Controller;

use Admin\Service\JpushService;

class PushMsgController extends BaseController {


    public function __construct() {
        parent::__construct();
        $this->jpushService = new JpushService();

        $typeArr = array(1=>'问题推送',2=>'逾期推送');
        $this->assign('typeArr',$typeArr);


    }


    public function setting() {
        $appkey = M('config')->where("name = 'jpush_appkey' and  inc_type = 'jpush' ")->find();
        $master_secret = M('config')->where("name = 'jpush_master_secret' and  inc_type = 'jpush' ")->find();
        if(IS_POST){
            $post = I('post.');
            if($post['id']){
                M('config')->where("name = 'jpush_appkey' and inc_type = 'jpush'")->save(array('value' => $post['jpush_appkey']));
                M('config')->where("name = 'jpush_master_secret' and inc_type = 'jpush'")->save(array('value' => $post['jpush_master_secret']));
            }else{
                M('config')->add(array('value' => $post['jpush_appkey'] , 'name' => 'jpush_appkey' , 'inc_type' => 'jpush'));
                M('config')->add(array('value' => $post['jpush_master_secret'] , 'name' => 'jpush_master_secret' , 'inc_type' => 'jpush'));
            }

            F('jpush', null, TEMP_PATH);
            $this->redirect('');
        }
        $this->assign('appkey',$appkey);
        $this->assign('master_secret',$master_secret);
        $this->display();
    }
    public function index() {

        $this->display();
    }
    public function ajax_index() {

        $nowPage = I('get.p', 1);
        $reg_id = I('post.reg_id', '');
        $username = I('post.username', '');

        $content = I('post.content', '');
        $type = I('post.type', '');

        $list = $this->jpushService->jpushList($username,$reg_id,$content,$type,1);
        $page = $list['data'];

        $busConductList = $this->jpushService->jpushList($username,$reg_id,$content,$type,0,$nowPage);

        $this->assign('page', $page->show());
        $this->assign('lists', $busConductList['data']);
        $this->display();
    }



    public function detail() {
        $id = I('get.id');
        $info = $this->jpushService->pushInfo($id);

        $this->assign('info', $info['info']);

        $this->assign('act','edit');
        $this->display();
    }


    public function handle() {
        if (IS_POST){

            $data = I('post.');
            $res = $this->jpushService->handle($data);

            if ($res['code'] == 200 && $data['act'] != 'del'){

                $this->success('操作成功', U('Admin/PushMsg/index'));
            } elseif($res['code'] != 200 && $data['act'] != 'del'){

                $this->error($res['msg'] ? $res['msg'] : '操作失败');
            } elseif ($data['act'] == 'del'){

                $this->ajaxReturn($res);
            }
        }
    }


}
